#!/bin/sh

INPUT_FILENAME=$1
OUTPUT_FILENAME="$(echo $INPUT_FILENAME | sed -e 's/.txt//g')"
NEW_STRING='_Free_Memory_info.txt'
OUTPUT_FILENAME="$OUTPUT_FILENAME$NEW_STRING"
count=0
while read LINE
do
  let count++
  regex=^Free\ Memory\ :
  if [[ $LINE =~ $regex ]]
  then
    RESULT_1="$( cut -d ":" -f2 <<< $LINE )"
    RESULT_2="$( cut -d " " -f1 <<< $RESULT_1 )"
    echo $RESULT_2 >> $OUTPUT_FILENAME
    echo write $RESULT_2 to output file.
  fi
done < "$INPUT_FILENAME"
echo -e "\nTotal $count Lines read. Save free memory info to $OUTPUT_FILENAME"
